﻿package  {
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flashx.textLayout.conversion.PlainTextExporter;
	
	
	public class Main extends MovieClip {
		private static const XML_PATH:String="data/dungeon.xml";
		
		
		var player:Player;
		var enemy;
		var constants:Variables;
		var room
		var gameObjects:Vector.<GameObject>;
		private var _map:Grid;
		
		public function Main() {
			// constructor code
			_map = new Grid();
			
			constants = new Variables();
			addChild(constants);
			addChild(_map);
			player = new Player(stage.stageWidth/2,(stage.stageHeight/2)+150,stage.stageWidth/Variables.CollumnsPerRoom - 5,stage.stageHeight/Variables.RowsPerRoom - 5,_map);
			//enemy = new Enemy(200,200,stage.stageWidth/Variables.CollumnsPerRoom,stage.stageHeight/Variables.RowsPerRoom,player);
			
			
			
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, player.keyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, player.keyUp);
			
			
			// load xml files
			GameDataStore.sharedInstance.loadXML(XML_PATH);
			GameDataStore.sharedInstance.addEventListener(GameDataStore.LOAD_COMPLETE, onXMLLoaded);
		}
		
		public function update(e:Event)
		{
			_map.Update();
			/*
			if(!enemy.dead){
				enemy.Update();
			}
			player.Update();
			*/
		}
		
		public function onXMLLoaded(e:Event)
		{
			
			_map.CreateRooms(stage.stageWidth,stage.stageHeight,player);
			_map.startRoom.player = player;
			//addChild(room);
			addChild(_map.startRoom);
			//player.init();
			//player.tiles = _map.startRoom.tiles2;
			//enemy.tiles = _map.startRoom.tiles2;
			addEventListener(Event.ENTER_FRAME, update);
		}
	}
	
}
